﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.BizRule;
using ECCSystem.EntityClass;
namespace ECCSystem
{
	public partial class frmEntrySearch : Form
	{
		int intMailCheck = 0;
		public frmEntrySearch()
		{
			InitializeComponent();
		}

		public frmEntrySearch(int MailCheckFlg)
		{
			intMailCheck = MailCheckFlg;
			InitializeComponent();
		}

		private void frmEntrySearch_Load(object sender, EventArgs e)
		{
			ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
			DateTime objDateTime = new DateTime();

            if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour <= 7)
			{
				objDateTime = DateTime.Now.AddDays(-1);
			}
			else
			{
				objDateTime = DateTime.Now;
			}
            dtpTime.Value = objDateTime;
			DataSet objDataSet = (DataSet)objClsTrackTaskBill.GetList(ClsCommon.gIntOperatorID, 1, objDateTime, intMailCheck);

			if (objDataSet.Tables.Count > 0)
			{
				DataTable objDataTable = new DataTable();
				objDataTable = objDataSet.Tables[0].Clone();
				objDataTable = objDataSet.Tables[0].Copy();
				DataView objDataView = objDataTable.DefaultView;
				objDataView.RowFilter = "TrackTaskBill_Status = 2";
				lblSend.Text = objDataView.Count.ToString();
				objDataView.RowFilter = "TrackTaskBill_Status = 3";
				lblSendAgain.Text = objDataView.Count.ToString();
				objDataView.RowFilter = "TrackTaskBill_Status = 4";
				lblHoldStatusCount.Text = objDataView.Count.ToString();
				objDataView.RowFilter = "TrackTaskBill_Status = 5";
				lblEndStatusCount.Text = objDataView.Count.ToString();
				lblSumCount.Text = objDataSet.Tables[0].Rows.Count.ToString();
				BindDataView(objDataSet.Tables[0]);
			}
			else
			{
				lblSend.Text = "0";
				lblSendAgain.Text = "0";
				lblHoldStatusCount.Text = "0";
				lblEndStatusCount.Text = "0";
				lblSumCount.Text = "0";
			}


			objDataSet = (DataSet)objClsTrackTaskBill.GetSysInfor();

			if (objDataSet.Tables.Count > 0)
			{
				BindDataCmb(objDataSet.Tables[0]);
			}

			if (intMailCheck == 1)
			{
				btnAdd.Enabled = false;
				btnDelete.Enabled = false;
				btnEdit.Text = "MailCheck";
			}
		}

		private void BindDataView(DataTable objDataTable)
		{
			dgList.DataSource = null;
			if (objDataTable.Rows.Count > 0)
			{
				dgList.AutoGenerateColumns = false;
				dgList.Sort(dgList.Columns["TrackTaskBill_EccDate"], ListSortDirection.Descending);
				dgList.DataSource = objDataTable;
				
			}
		}

		private void BindDataCmb(DataTable objDataTable)
		{
			if (objDataTable.Rows.Count > 0)
			{
				cmbFilter.DisplayMember = "Content";
				cmbFilter.ValueMember = "KeyID";
				cmbFilter.DataSource = objDataTable;
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
			DataSet objDataSet = (DataSet)objClsTrackTaskBill.GetList(ClsCommon.gIntOperatorID,
							Convert.ToInt32(cmbFilter.SelectedValue), dtpTime.Value, intMailCheck);

			if (objDataSet.Tables.Count > 0)
			{
				BindDataView(objDataSet.Tables[0]);
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			TrackTaskBillEntity objTrackTaskBillEntity = null;
			frmEntryInput objfrmEntryInput = new frmEntryInput(TypeTemp.Enum_TypeTempAdd, 
							objTrackTaskBillEntity, ClsEnumInputMode.Enum_TypeStrat,new DateTime());
			objfrmEntryInput.Show();
			this.Dispose();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{

			try
			{
				string strBusinessID;
				if (dgList.SelectedRows.Count <= 0)
				{
					MessageBox.Show("データを選んでください!", "ECC Information");
					return;
				}
				strBusinessID = Convert.ToString(dgList["TrackTaskBill_KeyID", dgList.CurrentRow.Index].Value);
				if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
				{
					ClsTrackTaskBill objClsTrackTaskBill = new ClsTrackTaskBill();
					objClsTrackTaskBill.DeleteTrackTaskBill(strBusinessID);
					DataSet objDataSet = (DataSet)objClsTrackTaskBill.GetList(ClsCommon.gIntOperatorID, 1, DateTime.Now, intMailCheck);

					if (objDataSet.Tables.Count > 0)
					{
						BindDataView(objDataSet.Tables[0]);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "ECC Catch Error");
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (!CheckRow())
			{
				return;
			}

			TrackTaskBillEntity objTrackTaskBillEntity = new TrackTaskBillEntity();

			objTrackTaskBillEntity = BindEntity(objTrackTaskBillEntity);
			frmEntryInput objfrmEntryInput;
			if (intMailCheck == 1)
			{
				objfrmEntryInput = new frmEntryInput(TypeTemp.Enum_TypeTempUpdate,
						objTrackTaskBillEntity, ClsEnumInputMode.Enum_TypeMailCheck, DateTime.Now);
			}
			else
			{
				objfrmEntryInput = new frmEntryInput(TypeTemp.Enum_TypeTempUpdate,
					objTrackTaskBillEntity, ClsEnumInputMode.Enum_TypeSendAgain, DateTime.Now);
			}
			objfrmEntryInput.Show();
			this.Dispose();
		}

		private TrackTaskBillEntity BindEntity(TrackTaskBillEntity objTrackTaskBillEntity)
		{
			objTrackTaskBillEntity.CategoryContent_KeyID = Convert.ToString(dgList["CategoryContent_KeyID", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.OvertimeBill_KeyID = Convert.ToString(dgList["OvertimeBill_KeyID", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_AppointStatus = Convert.ToInt32(dgList["TrackTaskBill_AppointStatus", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_EndTime = Convert.ToDateTime(dgList["TrackTaskBill_EndTime", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_EndTimeSpan = Convert.ToInt64(dgList["TrackTaskBill_EndTimeSpan", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_KeyID = Convert.ToString(dgList["TrackTaskBill_KeyID", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_OperatePerson = Convert.ToString(dgList["TrackTaskBill_OperatePerson", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_OvertimeStatus = Convert.ToInt32(dgList["TrackTaskBill_OvertimeStatus", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendNumber = Convert.ToInt32(dgList["TrackTaskBill_SendNumber", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendNumberAgain = Convert.ToInt32(dgList["TrackTaskBill_SendNumberAgain", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendTime = Convert.ToDateTime(dgList["TrackTaskBill_SendTime", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendTimeAgain = Convert.ToDateTime(dgList["TrackTaskBill_SendTimeAgain", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendTimeAgainSpan = Convert.ToInt64(dgList["TrackTaskBill_SendTimeAgainSpan", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_SendTimeSpan = Convert.ToInt64(dgList["TrackTaskBill_SendTimeSpan", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_StartDate = Convert.ToDateTime(dgList["TrackTaskBill_StartDate", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_Status = Convert.ToInt32(dgList["TrackTaskBill_Status", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.TrackTaskBill_UserID = Convert.ToInt32(dgList["TrackTaskBill_UserID", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.Category_Name = Convert.ToString(dgList["Category_Name", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.CategoryContent_Name = Convert.ToString(dgList["CategoryContent_Name", dgList.CurrentRow.Index].Value);
			objTrackTaskBillEntity.CategoryScore_KeyID = Convert.ToString(dgList["CategoryScore_KeyID", dgList.CurrentRow.Index].Value);
			return objTrackTaskBillEntity;
		}

		private void frmEntrySearch_FormClosing(object sender, FormClosingEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}

		private void dgList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (!CheckRow())
			{
				return;
			}

			TrackTaskBillEntity objTrackTaskBillEntity = new TrackTaskBillEntity();

			objTrackTaskBillEntity = BindEntity(objTrackTaskBillEntity);

			frmEntryInput objfrmEntryInput;

			if (intMailCheck == 1)
			{
				objfrmEntryInput = new frmEntryInput(TypeTemp.Enum_TypeTempUpdate,
						objTrackTaskBillEntity, ClsEnumInputMode.Enum_TypeMailCheck, DateTime.Now);
			}
			else
			{
				objfrmEntryInput = new frmEntryInput(TypeTemp.Enum_TypeTempUpdate,
					objTrackTaskBillEntity, ClsEnumInputMode.Enum_TypeSendAgain, DateTime.Now);
			}
			objfrmEntryInput.Show();
			this.Dispose();
		}

		private bool CheckRow()
		{
			if (dgList.SelectedRows.Count > 0)
			{
				if (Convert.ToInt32(dgList.CurrentRow.Cells["TrackTaskBill_Status"].Value) == (int)ClsEnumInputMode.Enum_TypeSend ||
					Convert.ToInt32(dgList.CurrentRow.Cells["TrackTaskBill_Status"].Value) == (int)ClsEnumInputMode.Enum_TypeSendAgain)
				{
					return true;
				}
				else if (Convert.ToInt32(dgList.CurrentRow.Cells["TrackTaskBill_Status"].Value) == (int)ClsEnumInputMode.Enum_TypeDepent ||
					Convert.ToInt32(dgList.CurrentRow.Cells["TrackTaskBill_Status"].Value) == (int)ClsEnumInputMode.Enum_TypeEnd)
				{
					return false;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}

		private void cmbFilter_SelectedValueChanged(object sender, EventArgs e)
		{
			btnSearch_Click(null, null);
		}
	}
}